package com.niiwoo.civet.trade.service.local.loan.credit;

import com.niiwoo.civet.trade.dao.entity.credit.CreditInvestigationPerson;
import com.niiwoo.civet.trade.dao.entity.credit.CreditProject;
import com.niiwoo.civet.trade.dao.entity.credit.CreditProjectGuarantee;
import com.niiwoo.civet.trade.dao.mapper.credit.CreditInvestigationPersonMapperExt;
import com.niiwoo.civet.trade.dao.mapper.credit.CreditProjectGuaranteeMapperExt;
import com.niiwoo.civet.trade.dao.mapper.credit.CreditProjectMapperExt;
import com.niiwoo.civet.trade.dto.request.loan.credit.INeedGuaranteeRequestDTO;
import com.niiwoo.civet.trade.dto.response.loan.credit.INeedGuaranteeResponseDTO;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

/**
 * 信用借本地服务
 * @author lichuanzhi
 * @date 2017/12/15
 */

@Slf4j
@Service
public class CreditLoanService {

    @Autowired
    private CreditProjectMapperExt creditProjectMapperExt;

    @Autowired
    private CreditProjectGuaranteeMapperExt guaranteeMapperExt;

    @Autowired
    private CreditInvestigationPersonMapperExt investigationPersonMapperExt;

    public PageResponseDTO<INeedGuaranteeResponseDTO> pageINeedGuarantee(INeedGuaranteeRequestDTO iNeedGuaranteeRequestDTO) {
        PageResponseDTO<INeedGuaranteeResponseDTO> pageResponse = new PageResponseDTO<>();
        int needGuaranteeCount = creditProjectMapperExt.countINeedGuarantee(iNeedGuaranteeRequestDTO);
        if (needGuaranteeCount <= iNeedGuaranteeRequestDTO.getOffset()) {
            return pageResponse;
        }

        List<INeedGuaranteeResponseDTO> responseDTOList = new ArrayList<>();
        List<CreditProject> creditProjects = creditProjectMapperExt.pageINeedGuarantee(iNeedGuaranteeRequestDTO);
        creditProjects.stream().forEach(creditProject -> {
            INeedGuaranteeResponseDTO responseDTO = new INeedGuaranteeResponseDTO();
            BeanUtils.copyProperties(creditProject, responseDTO);
            responseDTO.setGuaranteeUserHeadImages(guaranteeMapperExt.getGuaranteesByProjectId(creditProject.getProjectId())
                    .stream().map(CreditProjectGuarantee::getGuaranteeHeadImg).collect(toList()));
            Optional<CreditInvestigationPerson> investorsByProjectId = Optional.ofNullable(investigationPersonMapperExt
                    .getInvestorsByProjectId(creditProject.getProjectId()));
            responseDTO.setInvestigatorHeadImg(investorsByProjectId.map(CreditInvestigationPerson::getInvestigationHeadImg).orElse(null));
            responseDTO.setInvestigatorNickName(investorsByProjectId.map(CreditInvestigationPerson::getInvestigationNickName).orElse(null));
            responseDTOList.add(responseDTO);
        });

        pageResponse.setItems(responseDTOList);
        pageResponse.measureTotalPage(needGuaranteeCount,iNeedGuaranteeRequestDTO.getPageSize());
        return pageResponse;
    }

}
